package com.network.topo.snmp;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;

import com.network.util.snmp4j.SnmpGetResultSet;

public abstract class AbstractXxxTable
{
	private static final Logger logger = Logger.getLogger(AbstractXxxTable.class);
	protected SnmpGetResultSet resultSet = null;

	protected String getValue(String oid)
	{
		return getValue(oid, 0);
	}

	protected String getValue(String oid, int row)
	{
		String rtnValue = "";
		try
		{
			if (resultSet != null)
			{
				rtnValue = resultSet.getValue(oid, row);
			}
		}
		catch (RuntimeException e)
		{
			logger.info(e.getLocalizedMessage());
			e.printStackTrace();
		}
		return rtnValue;
	}

	protected int str2int(String input)
	{
		int rtnValue = 0;
		try
		{
			rtnValue = Integer.valueOf(input);
		}
		catch (NumberFormatException e)
		{
			//logger.info(e.getLocalizedMessage());
			//e.printStackTrace();
		}
		catch(Exception e){
			
		}
		return rtnValue;
	}

	protected long str2long(String input)
	{
		long rtnValue = 0;
		try
		{
			rtnValue = Long.valueOf(input);
		}
		catch (NumberFormatException e)
		{
			//logger.info(e.getLocalizedMessage());
			//e.printStackTrace();
		}
		catch(Exception e){
			//logger.info(e.getLocalizedMessage());
		}
		return rtnValue;
	}

	public void save2File(FileOutputStream fos)
	{
		if (fos != null && resultSet != null)
		{
			String lineTemp = "\n========================================\n";
			for (String oidKey : resultSet.getKeySet())
			{
				String rowData[] = resultSet.getQueryResult().get(oidKey);
				for (String element : rowData)
				{
					lineTemp = lineTemp + element + "\t";
				}
				lineTemp = lineTemp + "\n";
			}
			lineTemp = lineTemp + "========================================\n";
			if (lineTemp.equals("") == false)
			{
				try
				{
					fos.write(lineTemp.getBytes());
					fos.flush();
				}
				catch (IOException ex)
				{
					logger.info(ex.getLocalizedMessage());
					ex.printStackTrace();
				}
			}
		}
	}

	public SnmpGetResultSet getResultSet()
	{
		return resultSet;
	}

	public void setResultSet(SnmpGetResultSet resultSet)
	{
		this.resultSet = resultSet;
	}
}
